﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Forms;
using System.Security;
using System.Windows.Threading;

namespace MediaPlayerV2
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    /// 
    public partial class MainWindow : Window
    {
        private DispatcherTimer timer;
        bool isDragging = false;
        
        public MainWindow()
        {
            InitializeComponent();
        }

        private void ElementMediaOpened(object sender, RoutedEventArgs e)
        {
            timelineSlider.Maximum = mediaElement1.NaturalDuration.TimeSpan.TotalMilliseconds;

            this.timer = new DispatcherTimer();
            this.timer.Interval = new TimeSpan(0,0,0,1);
            this.timer.Tick += new EventHandler(timer_Tick);
            this.timer.Start();
        }

        private void timer_Tick(object sender, EventArgs e)
        {
            if (isDragging == false)
            {
                timelineSlider.Value = (mediaElement1.Position.Minutes * 60 + mediaElement1.Position.Seconds) * 1000;
                //Console.WriteLine(timelineSlider.Maximum + " / " + (mediaElement1.Position.Minutes * 60 + mediaElement1.Position.Seconds) * 1000);
            }
        }

        private void ElementMediaEnded(object sender, RoutedEventArgs e)
        {

        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.InitialDirectory = "c:\\";
            dlg.Filter = "(*.wmv, *.mp3, *.bmp, *.jpg)|*.bmp;*.jpg;*mp3;*.wmv|All Files (*.*)|*.*";
            dlg.RestoreDirectory = true;

            if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                string selectedFileName = dlg.FileName;
                /*FileNameLabel.Content = selectedFileName;*/
                mediaElement1.Source = new Uri(selectedFileName);
                mediaElement1.Play();
            }
        }

        // Play the media.
        void OnPlay(object sender, RoutedEventArgs e)
        {
            mediaElement1.Play();
        }

        // Pause the media.
        void OnPause(object sender, RoutedEventArgs e)
        {
            mediaElement1.Pause();
        }

        // Stop the media.
        void OnStop(object sender, RoutedEventArgs e)
        {
            mediaElement1.Stop();
        }

        // Change the volume of the media.
        private void ChangeMediaVolume(object sender, RoutedPropertyChangedEventArgs<double> args)
        {
            mediaElement1.Volume = (double)slider2.Value;
        }

        // Change the speed of the media.
        private void ChangeMediaSpeedRatio(object sender, RoutedPropertyChangedEventArgs<double> args)
        {
            mediaElement1.SpeedRatio = (double)slider1.Value;
        }

        private void tabControl1_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

        }

        private void timelineSlider_DragCompleted(object sender, System.Windows.Controls.Primitives.DragCompletedEventArgs e)
        {
            int SliderValue = (int)timelineSlider.Value;

            //Console.WriteLine(SliderValue + " / " + timelineSlider.Maximum);
            TimeSpan ts = new TimeSpan(0, 0, 0, 0, SliderValue);
            mediaElement1.Position = ts;
            isDragging = false;
        }

        private void timelineSlider_DragStarted(object sender, System.Windows.Controls.Primitives.DragStartedEventArgs e)
        {
            isDragging = true;
        }

    }
}
